/*
 * Project Name: showtao
 * File Name: Product.java
 * Class Name: Product
 *
 * Copyright 2014 showtao.com
 *
 * Licensed under the showtao.com
 *
 * http://showtao.com
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.showtao.front.domain;

import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.codehaus.jackson.annotate.JsonIgnore;

/**
 * Class Name: Product Description: TODO
 * 
 * @author
 * 
 */
@SuppressWarnings("serial")
@Entity
@Table(name = "st_product")
public class Product extends BaseDomain {

	@Id
	@GeneratedValue
	private Long id;

	@Column
	private String code;

	@ManyToOne(cascade = { CascadeType.MERGE, CascadeType.REFRESH }, fetch=FetchType.EAGER)
    @JoinColumn(name = "product_category_id")
	private ProductCategory category;

	@Column(name = "has_walking_box")
	private Boolean hasWalkingBox;

	@Column(name = "has_back_side")
	private Boolean hasBackSide;

	@Column(name = "has_special_notes")
	private Boolean hasSpecialNotes;

	@Column(name = "has_stereotype")
	private Boolean hasStereotype;

	@Column(name = "model_year")
	private String modelYear;
	
	@Column(name = "model_parameters")
	private String modelParameters;
	
	@Column(name = "model_factor")
	private Double modelFactor;
	
	@Column(name = "template_feature")
	private String templateFeature;
	
	@Column(name = "sewing_technology")
	private String sewingTechnology;
	
	@Column(name = "boxing_quantity")
	private Integer boxingQuantity;
	
	@Column(name = "notes")
	private String notes;
	
	@Column(name = "parameters")
	private String parameters;
	
	@Column(name = "dismounting_notes")
	private String dismountingNotes;
	
	@ManyToOne(cascade = { CascadeType.MERGE, CascadeType.REFRESH }, fetch=FetchType.EAGER)
	@JoinColumn(name = "mobile_intro_pic_id")
	private Resource mobileIntroPic;
	
	@ManyToOne(cascade = { CascadeType.MERGE, CascadeType.REFRESH }, fetch=FetchType.EAGER)
	@JoinColumn(name = "intro_pic_id")
	private Resource introPic;
	
	@ManyToOne(cascade = { CascadeType.MERGE, CascadeType.REFRESH }, fetch=FetchType.EAGER)
	@JoinColumn(name = "param_pic_id")
	private Resource paramPic;
	
	@ManyToOne(cascade = { CascadeType.MERGE, CascadeType.REFRESH }, fetch=FetchType.EAGER)
	@JoinColumn(name = "dismounting_pic_id")
	private Resource dismountingPic;
	
	@ManyToOne(cascade = { CascadeType.MERGE, CascadeType.REFRESH }, fetch=FetchType.EAGER)
	@JoinColumn(name = "merging_pic_id")
	private Resource mergingPic;

	@JsonIgnore
	@ManyToOne(cascade = { CascadeType.MERGE, CascadeType.REFRESH }, optional = false)
	@JoinColumn(name = "model_id")
	private VehicleModel model;

	@ManyToMany(cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH }, fetch=FetchType.EAGER)
    @JoinTable(name = "st_product_feature_xref",
        joinColumns = {@JoinColumn(name = "product_id", referencedColumnName = "id")}, 
        inverseJoinColumns = {@JoinColumn(name = "feature_id", referencedColumnName = "id")})
	@OrderBy("categoryNumber")
	private List<VehicleModelFeature> features;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public ProductCategory getCategory() {
        return category;
    }

    public void setCategory(ProductCategory category) {
        this.category = category;
    }

    public Boolean getHasWalkingBox() {
		return hasWalkingBox;
	}

	public void setHasWalkingBox(Boolean hasWalkingBox) {
		this.hasWalkingBox = hasWalkingBox;
	}

	public Boolean getHasBackSide() {
		return hasBackSide;
	}

	public void setHasBackSide(Boolean hasBackSide) {
		this.hasBackSide = hasBackSide;
	}

	public Boolean getHasSpecialNotes() {
		return hasSpecialNotes;
	}

	public void setHasSpecialNotes(Boolean hasSpecialNotes) {
		this.hasSpecialNotes = hasSpecialNotes;
	}

	public Boolean getHasStereotype() {
		return hasStereotype;
	}

	public void setHasStereotype(Boolean hasStereotype) {
		this.hasStereotype = hasStereotype;
	}

	public String getModelYear() {
		return modelYear;
	}

	public void setModelYear(String modelYear) {
		this.modelYear = modelYear;
	}

	public String getModelParameters() {
		return modelParameters;
	}

	public void setModelParameters(String modelParameters) {
		this.modelParameters = modelParameters;
	}

	public Double getModelFactor() {
		return modelFactor;
	}

	public void setModelFactor(Double modelFactor) {
		this.modelFactor = modelFactor;
	}

	public String getTemplateFeature() {
		return templateFeature;
	}

	public void setTemplateFeature(String templateFeature) {
		this.templateFeature = templateFeature;
	}

	public String getSewingTechnology() {
		return sewingTechnology;
	}

	public void setSewingTechnology(String sewingTechnology) {
		this.sewingTechnology = sewingTechnology;
	}

	public Integer getBoxingQuantity() {
		return boxingQuantity;
	}

	public void setBoxingQuantity(Integer boxingQuantity) {
		this.boxingQuantity = boxingQuantity;
	}

	public String getNotes() {
		return notes;
	}

	public void setNotes(String notes) {
		this.notes = notes;
	}

	public String getParameters() {
		return parameters;
	}

	public void setParameters(String parameters) {
		this.parameters = parameters;
	}

	public String getDismountingNotes() {
		return dismountingNotes;
	}

	public void setDismountingNotes(String dismountingNotes) {
		this.dismountingNotes = dismountingNotes;
	}

	public Resource getMobileIntroPic() {
		return mobileIntroPic;
	}

	public void setMobileIntroPic(Resource mobileIntroPic) {
		this.mobileIntroPic = mobileIntroPic;
	}

	public Resource getIntroPic() {
		return introPic;
	}

	public void setIntroPic(Resource introPic) {
		this.introPic = introPic;
	}

	public Resource getParamPic() {
		return paramPic;
	}

	public void setParamPic(Resource paramPic) {
		this.paramPic = paramPic;
	}

	public Resource getDismountingPic() {
		return dismountingPic;
	}

	public void setDismountingPic(Resource dismountingPic) {
		this.dismountingPic = dismountingPic;
	}

	public Resource getMergingPic() {
		return mergingPic;
	}

	public void setMergingPic(Resource mergingPic) {
		this.mergingPic = mergingPic;
	}
	
	public int getPicCount(){
		int c = 0;
		if(this.getIntroPic()!=null){
			c++;
		}
		if(this.getParamPic()!=null){
			c++;
		}
		if(this.getDismountingPic()!=null){
			c++;
		}
		if(this.getMergingPic()!=null){
			c++;
		}
		return Math.max(0, c);
	}

	public VehicleModel getModel() {
		return model;
	}

	public void setModel(VehicleModel model) {
		this.model = model;
	}

	public List<VehicleModelFeature> getFeatures() {
		return features;
	}

	public void setFeatures(List<VehicleModelFeature> features) {
		this.features = features;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		} else if (!this.getClass().isInstance(obj)) {
			return false;
		}
		return new EqualsBuilder().append(id, ((Product) obj).getId())
				.isEquals();
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder().append(id).toHashCode();
	}
}
